package com.wangbo.metadata;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.imaging.png.PngMetadataReader;
import com.drew.imaging.png.PngProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifDirectoryBase;

import java.io.File;
import java.io.IOException;

public class TestPng {
    public static void main(String[] args) throws PngProcessingException, IOException, MetadataException, JpegProcessingException {
        test01(new File("D:\\temp\\微信图片_20211026152434.jpg"));
    }

    /**
     * 获取图片方向
     */
    public static void test01(File file) throws PngProcessingException, IOException, MetadataException, JpegProcessingException {
//        Metadata metadata = PngMetadataReader.readMetadata(file);
        Metadata metadata = JpegMetadataReader.readMetadata(file);
        int orientation = 0;
        Directory directory = metadata.getFirstDirectoryOfType(ExifDirectoryBase.class);
        if (directory != null && directory.containsTag(ExifDirectoryBase.TAG_ORIENTATION)) {
            orientation = directory.getInt(ExifDirectoryBase.TAG_ORIENTATION); // 获取图片方向
        }
    }
}
